app.controller('CarModelCtrl',[
    '$scope','notification','carModelService',
    function ($scope,notification,carModelService) {
        var selectedBrand=undefined;

        carModelService.loadBrandListAsync()
            .then(function(result){
                $scope.brandList=result;
            },notification.error);

        /**
         * 选择Tag
         */
        $scope.selectTag=function(tag){
            $scope.selectedTag=tag;
        };
        /**
         * 选择品牌
         */
        $scope.onSelectBrand=function(brand){
            selectedBrand=brand;

            carModelService.loadCarsOfBrandAsync(brand.Id)
                .then(function(result){

                },notification.error);
        };
    }
]);

app.factory('carModelService',[
    '$http','$q',
    function($http,$q){
        var self={};

        self.loadBrandListAsync=function(){
            var q= $q.defer();
            $http({
                url:'/api/car/brands'
            }).success(function(result){
                if(result.Code>=0){
                    q.resolve(result.Data);
                }else{
                    q.reject(result.Message);
                }
            }).error(function(x){
                q.reject('加载品牌失败');
                console.error(x);
            });
            return q.promise;
        };

        self.loadCarsOfBrandAsync=function(brandId){
            var q= $q.defer();
            $http({
                url:'/api/car/CarsOfBrand',
                params:{brandId:brandId}
            }).success(function(result){
                if(result.Code>=0){
                    q.resolve(result.Data);
                }else{
                    q.reject(result.Message);
                }
            }).error(function(x){
                q.reject('加载车型失败');
                console.error(x);
            });
            return q.promise;
        };

        return self;
    }
]);